using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Xml;

using UNLV.IAP.WebControls;
using ScenarioFramework.Portal;

namespace ScenarioFramework.Portal.SF.Reports
{
	/// <summary>
	/// Summary description for Process.
	/// </summary>
	public partial class Process : System.Web.UI.Page
	{
	
		private string sFilterCondition = string.Empty;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			this.btnFilter.Attributes.Clear();
			this.btnFilter.Attributes.Add("onclick","this.value='Please wait...';this.disabled = true;" + this.GetPostBackEventReference (this.btnFilter) + ";");

			if ( Page.IsPostBack)
			{
				sFilterCondition = (string)ViewState["FilterCondition"];
			}
			else
			{
				ViewState["FilterCondition"] = string.Empty;

				string sFile = Server.MapPath(".\\config_process\\process_fields.config");
				if ( System.IO.File.Exists ( sFile ) )
				{
					this.SqlWhereBuilder1.LoadFieldsFromXml ( sFile);
					this.SqlWhereBuilder1.Visible=true;
				}
				else
				{
					this.SqlWhereBuilder1.Visible=false;
				}
			}
		}

		

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
		
		private void MakeQueries()
		{
			string sFile = Server.MapPath(".\\config_process\\ConfiguredQuery.config");
			if ( System.IO.File.Exists ( sFile ) )
			{
				XmlDocument x = new XmlDocument();
				x.Load (sFile);

				foreach ( XmlNode xmlNodeQuery in x.DocumentElement.ChildNodes )
				{
					if ( xmlNodeQuery.Name == "Query")
					{
						LinkButton linkButton = new LinkButton();
						linkButton.ID = xmlNodeQuery.Attributes["Name"].Value;
						linkButton.Text = xmlNodeQuery.Attributes["Alias"].Value;
						linkButton.Font.Name="Tahoma";
						//						linkButton.Font.Size.Unit = FontUnit.XXSmall;
						linkButton.Click += new System.EventHandler(this.LinkbuttonQuery_Click);
						this.placeholderQuery.Controls.Add ( new LiteralControl ("<LI>") );
						this.placeholderQuery.Controls.Add ( linkButton);
					}
				}
			}
		}


		protected void btnFilter_Click(object sender, System.EventArgs e)
		{
			this.gridDocuments.CurrentPageIndex = 0;

			string sWhere = this.SqlWhereBuilder1.GetWhereClause();

			ViewState["WhereCondition"]= sWhere;
			this.ListRecords(sWhere);

			Session["Where"] = sWhere;
		}

		private void ListRecords(string sWherePlus)
		{
			if ( sWherePlus == string.Empty )
			{
				string sWhereFromViewState = (string)ViewState["WhereCondition"];

				if (sWhereFromViewState != null)
				{
					sWherePlus = sWhereFromViewState;
				}
			}

			string sWhere = string.Empty;

			if ( sWherePlus.CompareTo ( string.Empty ) != 0)
			{
				if ( this.sFilterCondition != string.Empty )
				{
					sWhere = "((" + sWherePlus + ") and (" + this.sFilterCondition + "))";
				}
				else
				{
					sWhere = sWherePlus;
				}
			}
			else
			{
				sWhere = this.sFilterCondition;
			}

            String sSQL = "select PROCESS_ID as ID, Name, Status, ParentProcess from sf_workflowprocesses";
            //this.gridDocuments.DataSource = Global.SFDatabase.GetProcesses (sSQL, sWhere, null);
			this.gridDocuments.DataBind();
			
		}

		private void gridDocuments_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			gridDocuments.CurrentPageIndex = e.NewPageIndex;
			this.ListRecords (string.Empty);
		}

		private void LinkbuttonQuery_Click(object sender, System.EventArgs e)
		{
			this.SqlWhereBuilder1.Conditions.Clear();

			
			LinkButton linkButton = (LinkButton)sender;
			this.AddConditions(linkButton.ID);

			// now continue as if we had clicked the "Display Winners" button
			this.btnFilter_Click(null, null);
		}

		private SqlWhereBuilderCondition GetCondition (XmlNode xmlNode)
		{
			string sFieldId = string.Empty;
			string sOperator = string.Empty;
			string sAndOr = string.Empty;

			XmlAttribute xmlAttribute = null;
			for ( int j=0; j<xmlNode.Attributes.Count;j++)
			{
				xmlAttribute = xmlNode.Attributes[j];
				if ( xmlAttribute.Name == "fieldId")
				{
					sFieldId = xmlAttribute.Value;
				}
				else if ( xmlAttribute.Name == "operator")
				{
					sOperator = xmlAttribute.Value;
				}
				else if ( xmlAttribute.Name == "andOr")
				{
					sAndOr = xmlAttribute.Value;
				}
			}
			return new SqlWhereBuilderCondition(sFieldId, sOperator, sAndOr);
		}

		private SqlWhereBuilderValue GetValue(XmlNode xmlValue)
		{
			string sName = string.Empty;
			string sFriendlyName = string.Empty;
			string sValue = string.Empty;

			XmlAttribute xmlAttributeValue=null;
			for ( int jv=0; jv<xmlValue.Attributes.Count;jv++)
			{
				xmlAttributeValue = xmlValue.Attributes[jv];
				if ( xmlAttributeValue.Name == "name")
				{
					sName = xmlAttributeValue.Value;
				}
				else if ( xmlAttributeValue.Name == "fiendlyValue")
				{
					sFriendlyName = xmlAttributeValue.Value;
				}
				else if ( xmlAttributeValue.Name == "value")
				{
					sValue = xmlAttributeValue.Value;
				}
			}
			return new SqlWhereBuilderValue(sName, sValue, sFriendlyName);
		}

		private void AddConditions(string sQuery)
		{
			string sFile = Server.MapPath(".\\config_process\\ConfiguredQuery.config");
			if ( System.IO.File.Exists ( sFile ) )
			{
				XmlDocument x = new XmlDocument();
				x.Load (sFile);

				foreach ( XmlNode xmlNodeQuery in x.DocumentElement.ChildNodes )
				{
					if ( ( xmlNodeQuery.Name == "Query") && ( xmlNodeQuery.Attributes["Name"].Value == sQuery ) )
					{
						foreach (XmlNode xmlCondition in xmlNodeQuery.ChildNodes )
						{
							SqlWhereBuilderCondition condition = this.GetCondition (xmlCondition);

							foreach ( XmlNode xmlValues in xmlCondition.ChildNodes)
							{
								foreach ( XmlNode xmlValue in xmlValues.ChildNodes)
								{
									condition.Values.Add ( this.GetValue(xmlValue));
								}
							}
							this.SqlWhereBuilder1.Conditions.Add(condition);
						}
					}
				}
			}		
		}

	}
}
